//
//  LKStatusViewModel.swift
//  LKWeibo
//
//  Created by like on 16/8/13.
//  Copyright © 2016年 like. All rights reserved.
//

import UIKit

class LKStatusViewModel: NSObject {
//创建model属性
    var statusModel: LKStatusModel?
    //等级图片  1 2 3
    var mbrankImage: UIImage?
    //认证图片   兔女郎
    var verifiedImage: UIImage?
    
    // 微博来源
    var sinaSourceStr: String?
    
    
    // 转发
    var repostsCountStr: String?
    // 评论
    var commentsCountStr: String?
    // 赞
    var attitudesCountStr: String?
    
    
    
    
    init(statusModel: LKStatusModel) {
        super.init()
        self.statusModel = statusModel
        mbrankImage = dealMbrankImage(statusModel.user?.mbrank ?? 0)
        verifiedImage = dealverifiedImage(statusModel.user?.verified ?? -1)
        //来源
        sinaSourceStr = dealSinaSourceStr(statusModel.source ?? "")
        //转发。评论，赞
        repostsCountStr = getSinaCountStr(statusModel.reposts_count ?? 0, title: "转发")
        commentsCountStr = getSinaCountStr(statusModel.comments_count ?? 0, title: "评论")
        attitudesCountStr = getSinaCountStr(statusModel.attitudes_count ?? 0, title: "赞")
        
 
    }
    
}
// 处理底部视图 转发 评论 赞
extension LKStatusViewModel {
    
    /*
     - 底部视图的转发 评论 赞  业务逻辑
     - 如果 count <= 0  直接显示 转发 评论 赞
     - 如果 count > 0 && count < 10000  是多少显示多少  示例  8888  8888
     - 如果 count >= 10000   示例 12000   显示  1.2万
     - 如果 count%10000 == 0   示例 20000  显示  2万
     */
    
    func getSinaCountStr(count: Int, title: String) -> String{
        if count <= 0 {
            return title
        }else if count > 0 && count < 10000{
            return "\(count)"
        }else {
            let cFloat = CGFloat(count/1000)/10
            var cFloatStr = "\(cFloat)万"
            // 是否包含".0"
            if cFloatStr.containsString(".0") {
                
                // 字符串替换
                cFloatStr = cFloatStr.stringByReplacingOccurrencesOfString(".0", withString: "")
            }
            
            return cFloatStr
        }
    }
}



//处理 来源
extension LKStatusViewModel{
    func dealSinaSourceStr(source: String) -> String{
        //        printLog("哈哈哈")
        // 判断字符串是否包含"> 特殊字符
        if source.containsString("\">") {
            // 获取range
            let startRange = source.rangeOfString("\">")!
            let endRange = source.rangeOfString("</")!
            // 字符串截取
            let result = source.substringWithRange(startRange.endIndex..<endRange.startIndex)
            return "来自 \(result)"
        }
        // 如果为空字符串
        return "来自星星的你"
    }
}






//处理cell中 业务逻辑，会员等级，认证
extension LKStatusViewModel{
    
    // 处理微博等级image/// 会员等级 1-6
    func dealMbrankImage(mbrank: Int) -> UIImage?{
        if mbrank > 0 && mbrank < 7 {
            return UIImage(named: "common_icon_membership_level\(mbrank)")
        }else{
            return UIImage(named: "common_icon_membership_expired")
        }
        
    }
    
    
    func dealverifiedImage(verified: Int) -> UIImage?{
        switch verified {
        case 1:
            return UIImage(named: "avatar_vip")
        case 2,3,5:
            return UIImage(named: "avatar_grassroot")
        case 220:
            return UIImage(named: "avatar_enterprise_vip")
        default:
            return UIImage(named: "avatar_vgirl")
        }
        
    }
    
    
}